/*
  jQuery ui.openidpickr - 

  (c) Matt Nowack, John Keyes

    Attributed Source 
  (c) Maxime Haineault <haineault@gmail.com>  (timepickr)
  http://haineault.com

  MIT License (http://www.opensource.org/licenses/mit-license.php
  
  Dependencies
  ------------
  - jquery.utils.js
  - jquery.strings.js
  - jquery.ui.js
  - ui.dropslide.js
  
*/

(function($) {
    $.tpl('openidpickr.menu', '<span class="ui-helper-reset ui-dropslide ui-openidpickr ui-widget" />');
    $.tpl('openidpickr.row', '<ol class="ui-openidpickr" />');
    $.tpl('openidpickr.button', '<li class="{className:s}"><span><img src="{label:s}" alt="{href:s}" style="width: 16px; height: 16px;"/></span></li>');

    $.widget('ui.openidpickr', {
        _init: function() {
            var ui = this;
            var menu = ui._buildMenu();
            var element = ui.element;
            element.data('openidpickr.initialValue', element.val());
            menu.insertAfter(ui.element);
            element
                .addClass('ui-openidpickr')
                .dropslide(ui.options.dropslide)
                .bind('select', ui.select);

            element.blur(function(e) {
                $(this).dropslide('hide');
                if (ui.options.resetOnBlur) {
                    $(this).val($(this).data('openidpickr.initialValue'));
                }
            });

            if (ui.options.val) {
                element.val(this.options.val);
            }

            if (ui.options.handle) {
                $(this.options.handle).click(function() {
                    $(element).dropslide('show');
                });
            }

            if (ui.options.resetOnBlur) {
                menu.find('li > span').bind('mousedown.openidpickr', function() {
                    $(element).data('openidpickr.initialValue', $(element).val());
                });
            }
            if (ui.options.updateLive) {
                menu.find('li').bind('mouseover.openidpickr', function() {
                    $(element).openidpickr('update');
                });
            }

            element.dropslide('redraw');
            element.data('openidpickr', this);
        },

        update: function() {
            var v = $('.ui-openidpickr').filter('.hover').children().children().attr('alt').replace(/{username}/, 
                        ($('.jq_openid_username').val() == "Username" ? "[provide a username]" : $('.jq_openid_username').val()));
            $(this.element).val(v);
        },

        select: function(e) {
            var dropslide = $(this).data('dropslide');
            $(dropslide.element).openidpickr('update');
            e.stopPropagation();
        },

        activate: function() {
            this.element.dropslide('activate');
        },

        destroy: function() {
            this.element.dropslide('destroy');
        },

        /* UI private methods */

        _createButton: function(src, href) {
            var o = src;
            var cn = 'ui-openidpickr';
            return $.tpl('openidpickr.button', { href: href, className: cn, label: o });
        },

        _createRow: function() {
            var row = $.tpl('openidpickr.row');
            row.append(this._createButton('http://google.com/favicon.ico', 'https://www.google.com/accounts/o8/id'));
            row.append(this._createButton('http://yahoo.com/favicon.ico', 'https://www.yahoo.com'));
            row.append(this._createButton('http://aol.com/favicon.ico', 'http://openid.aol.com/{username}'));
            row.append(this._createButton('http://myopenid.com/favicon.ico', 'http://{username}.myopenid.com'));
            row.append(this._createButton('http://livejournal.com/favicon.ico', 'http://{username}.livejournal.com'));
            row.append(this._createButton('http://flickr.com/favicon.ico', 'http://flickr.com/{username}/'));
            row.append(this._createButton('http://technorati.com/favicon.ico', 'http://technorati.com/people/technorati/{username}'));
            row.append(this._createButton('http://wordpress.com/favicon.ico', 'http://{username}.wordpress.com'));
            row.append(this._createButton('http://blogspot.com/favicon.ico', 'http://{username}.blogspot.com'));
            row.append(this._createButton('http://verisign.com/favicon.ico', 'http://{username}.pip.versignlabs.com'));
            row.append(this._createButton('http://claimid.com/favicon.ico', 'http://claimid.com/{username}'));
            return row;
        },

        _getIcons: function() {
            var o = [], opt = this.options;
            o.push(this._createRow());
            return o;
        },

        _buildMenu: function() {
            var menu = $.tpl('openidpickr.menu');
            var ranges = this._getIcons();

            $.each(ranges, function(idx, val) {
                menu.append(val);
            });
            return menu;
        }
    });

    // These properties are shared accross every instances of openidpickr 
    $.extend($.ui.openidpickr, {
        version: '@VERSION',
        eventPrefix: '',
        getter: '',
        defaults: {
            convention: 24, // 24, 12
            dropslide: { trigger: 'focus' },
            format12: '{h:02.d}:{m:02.d} {suffix:s}',
            format24: '{h:02.d}:{m:02.d}',
            handle: false,
            hours: true,
            minutes: true,
            seconds: false,
            prefix: ['am', 'pm'],
            suffix: ['am', 'pm'],
            rangeMin: $.range(0, 60, 15),
            rangeSec: $.range(0, 60, 15),
            updateLive: true,
            resetOnBlur: true,
            val: false
        }
    });

})(jQuery);
